package com.gege.homework8;

import com.gege.homework2.Klass;
import com.gege.homework2.School;
import com.gege.homework2.Student;
import com.gege.homework8.prop.SpringBootPropertiesConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author gechenpeng
 * @date 2021-04-16
 */
@Configuration
@EnableConfigurationProperties(SpringBootPropertiesConfiguration.class)
@ConditionalOnProperty(prefix = "spring.school", name = "enabled", havingValue = "true", matchIfMissing = true)
public class SchoolAutoConfiguration {

    private final SpringBootPropertiesConfiguration props;

    public SchoolAutoConfiguration(SpringBootPropertiesConfiguration props) {
        this.props = props;
    }

    @Bean
    public Student student1() {
        return new Student(Integer.valueOf((String) props.getProps().get("student1.id")),
                (String) props.getProps().get("student1.name"));
    }

    @Bean
    public Student student2() {
        return new Student(Integer.valueOf((String) props.getProps().get("student2.id")),
                (String) props.getProps().get("student2.name"));
    }

    @Bean
    @Autowired(required = false)
    public Klass klass(List<Student> studentList) {
        Klass klass = new Klass();
        klass.setStudents(studentList);
        return klass;
    }

    @Bean
    public School mySchool() {
        return new School();
    }

}
